import 'package:environmental_les_app/common/style/colours.dart';
import 'package:environmental_les_app/common/widgets/common_widget.dart';
import 'package:environmental_les_app/page/task_list/task_detail/widgets/text_bg_container.dart';
import 'package:environmental_les_app/page/task_list/widgets/basic_info_card.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import 'index.dart';

class EventDetailPage extends GetView<EventDetailController> {
  const EventDetailPage({super.key});

  // 主视图
  Widget _buildView() {
    return Column(
      children: [
        const MyPageAppBar(
          title: "事件详情",
          backgroundColor: Colors.transparent,
        ),
        Expanded(
          child: SingleChildScrollView(
            child: Column(
              children: [
                const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 15),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "已办结",
                        style: TextStyle(
                          fontSize: 24,
                          color: Colors.black,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      vGap5,
                      Row(
                        children: [
                          Text(
                            "张小猪  2025-08-22 11:23:19",
                            style: TextStyle(
                              fontSize: 14,
                              color: Colours.textC4C,
                            ),
                          ),
                          hGap8,
                          Text(
                            "创建",
                            style: TextStyle(
                              fontSize: 14,
                              color: Colours.text09C,
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                vGap10,
                _buildTaskInfoCard(),
                _buildSupervisionInfoCard(),
                _buildProcessFlowCard(),
              ],
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildTaskInfoCard() {
    bool isExpanded = controller.expansionState['taskInfo'] ?? true;
    return BasicInfoCard(
      title: "事件信息",
      cardKey: 'taskInfo',
      isExpanded: isExpanded,
      onTap: () => controller.toggleCard('taskInfo'),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: Colours.bgF4F7FE,
                  borderRadius: BorderRadius.circular(4),
                  border: Border.all(color: Colours.lineB7C0D7),
                ),
                child: const Text(
                  "大气污染",
                  style: TextStyle(
                      color: Colours.appMain,
                      fontSize: 13,
                      fontWeight: FontWeight.w500),
                ),
              ),
              hGap8,
              const Text(
                "RW453564333",
                style: TextStyle(
                    color: Colours.textC4C,
                    fontSize: 15,
                    fontWeight: FontWeight.w500),
              )
            ],
          ),
          vGap15,
          buildAddressInfoRow("巡查点位:", "准格尔旗薛家湾镇湾镇环保局"),
          vGap5,
          buildAddressInfoRow("详细地址:", "准格尔旗薛家湾镇环保局准格尔旗薛家湾镇环保局"),
          vGap10,
          TextBgContainer(
            child: Column(
              children: [
                buildInfoRow("巡查开始时间:", "2025-08-22 11:29:55"),
                vGap5,
                buildInfoRow("巡查截止时间:", "2025-08-22 11:29:55"),
                vGap5,
                buildInfoRow("巡查结果:", "正常"),
              ],
            ),
          ),
          vGap10,
          TextBgContainer(
              child: buildExplanationInfoRow(
                  "巡查描述", "这是具体的描述信息这是具体的描述信息这是具体的描述信息")),
          vGap10,
          TextBgContainer(
              child:
                  buildAddressInfoRow("备注:", "请在周边100米范围内进行巡备注备注备注备注备注备注备注查")),
          vGap10,
          TextBgContainer(
              child: Column(
            children: [
              Visibility(
                  visible: false, child: buildAddressInfoRow("附件:", "暂无")),
              fileListItem(),
              fileListItem(),
              fileListItem(),
              vGap10,
              SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Row(
                  children: [
                    imageListItem(),
                    imageListItem(),
                    imageListItem(),
                    imageListItem(),
                    imageListItem(),
                  ],
                ),
              )
            ],
          )),
          vGap10,
          TextBgContainer(
            child: Column(
              children: [
                buildInfoRow("事件接收单位:", "准格尔旗薛家湾镇环保局"),
                vGap5,
                buildInfoRow("事件接收人:", "准格尔旗薛家湾镇环保局"),
                vGap5,
                buildInfoRow("处理时间:", "2025-08-22 11:29:55"),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 文件列表项
  Widget fileListItem() {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 12),
      decoration: const BoxDecoration(
        border: Border(
          bottom: BorderSide(color: Colours.lineE5E6EB, width: 1),
        ),
      ),
      child: Row(
        children: [
          ImageWidget.loadAssetImage(
            'ic_library_file',
            width: 24,
            height: 24,
          ),
          hGap10,
          const Expanded(
            child: Text(
              "巡查报告.pdf",
              style: TextStyle(fontSize: 15, color: Colours.text254),
              overflow: TextOverflow.ellipsis,
            ),
          ),
          GestureDetector(
            onTap: () {
              // 打开文件
            },
            child: const Text(
              "查看",
              style: TextStyle(
                  fontSize: 15,
                  decoration: TextDecoration.underline,
                  decorationColor: Colours.appMain,
                  color: Colours.appMain,
                  fontWeight: FontWeight.w500),
            ),
          )
        ],
      ),
    );
  }

  // 图片列表项
  Widget imageListItem() {
    return Container(
      margin: const EdgeInsets.only(right: 10),
      child: ImageWidget.loadNetImage(
        "https://gips1.baidu.com/it/u=1658389554,617110073&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960",
        width: 80,
        height: 80,
        type: ImageTransformerType.rect,
      ),
    );
  }

  Widget _buildSupervisionInfoCard() {
    bool isExpanded = controller.expansionState['supervisionInfo'] ?? true;
    return BasicInfoCard(
      title: "督办信息",
      cardKey: 'supervisionInfo',
      onTap: () => controller.toggleCard('supervisionInfo'),
      isExpanded: isExpanded,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: Colours.bgF4F7FE,
                  borderRadius: BorderRadius.circular(4),
                  border: Border.all(color: Colours.lineB7C0D7),
                ),
                child: const Text(
                  "执法大队",
                  style: TextStyle(
                      color: Colours.appMain,
                      fontSize: 12,
                      fontWeight: FontWeight.w500),
                ),
              ),
              hGap8,
              const Text(
                "猩猩怪 (督办人)",
                style: TextStyle(
                    color: Colours.textC4C,
                    fontSize: 15,
                    fontWeight: FontWeight.w500),
              )
            ],
          ),
          vGap10,
          buildAddressInfoRow("督办时间:", "2025-08-21 14:44"),
          vGap10,
          TextBgContainer(
              child: buildExplanationInfoRow(
                  "督办内容", "这是具体的描述信息这是具体的描述信息这是具体的描述信息")),
        ],
      ),
    );
  }

  Widget _buildProcessFlowCard() {
    bool isExpanded = controller.expansionState['processFlow'] ?? true;
    return BasicInfoCard(
      title: "处理流程",
      cardKey: 'processFlow',
      onTap: () => controller.toggleCard('processFlow'),
      isExpanded: isExpanded,
      child: Column(
        children: [
          buildProcessStep("02", "创建任务", "2024/11/11 11:11:11", "值守中心", "王二",
              "任务说明这是说明说明这是说明说明这是说明说明", "执法大队部门", true),
          buildProcessStep("01", "驳回", "2024/11/11 11:11:11", "值守中心", "王二",
              null, "执法大队部门", false),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<EventDetailController>(
      init: EventDetailController(),
      id: "event_detail",
      builder: (_) {
        return Scaffold(
          body: Stack(
            children: [
              Container(
                height: 200,
                decoration: BoxDecoration(
                    image: DecorationImage(
                  image: AssetImage(ImageUtils.getImgPath("bg_title_top")),
                  fit: BoxFit.cover,
                )),
              ),
              _buildView(),
            ],
          ),
        );
      },
    );
  }
}
